(function() {

	layui.use(['table', 'util', 'element', 'form', 'laydate'], function() {
		var table = layui.table;
		var element = layui.element;
		var form = layui.form;
		var laydate = layui.laydate;
		var util = layui.util;
		var $ = layui.jquery;
		var url = GlobalSiteUrl + "/admin/selectSuggestion";
		
		//第一个实例
		table.render({
			elem: '#table',
			toolbar: '#toolbarDemo',
			width: 'auto',
			height: 'full - 100',
			url: url, //数据接口
			response: {
				statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
			},
			parseData: function(res) {
				if (res.data == null) {
					alert(res.msg)
					location.reload();
					return;
				} else {
					var data = res.data.list;
          data.forEach((element) => {
            element["create_time"] = formatTime(element["create_time"]);
          });
					if (res.data.total===0) {
						return {
							"code": res.status,
							"msg": res.msg,
							"data": res.data.list,
							"count": 1
						}
					} else {
						return {
							"code": res.status,
							"msg": res.msg,
							"data": res.data.list,
							"count": res.data.total
						}
					}
				}
			},
			page: true, //开启分页
			method: 'get',
			id: 'reloadTable',
			cols: [
				[ //表头
					{
						field: 'id',
						title: 'id',
						width: 80,
						align: 'center',
						sort: true
					}, {
						field: 'text',
						title: '用户意见',
						align: 'center',
						width: 500
					}, {
						field: 'name',
						title: '用户',
						align: 'center',
						width: 200,
					},
					{
						field: 'create_time',
						title: '创建时间',
						align: 'center',
						width: 227,
						sort: true
					},
					{
						fixed: 'right',
						align: 'center',
						title: '操作',
						toolbar: '#barDemo',
						width: 200
					}
				]
			]
		});
		table.on('tool(table)', function(obj) {
		
			var data = obj.data;
		
			if (obj.event === 'img') {
				var img = ""
				var images = data.images
				for (var i in images) {
					img += "<img src='" + images[i].url + "' class='img'>"
				}
				var image = "<div class='" + "layui-upload-list" + "'id='" + "demo2" + "'>" + img + "</div>"
				layer.open({
					area: ['1000px', '500px'],
					type: 1,
					title: "查看图片",
					value: data.images[0],
					content: image,
					btn: '关闭',
					yes: function(index) {
						layer.closeAll();
					}
				});
			}
		
			// 开始验机
			if (obj.event === 'del') {
				var url = GlobalSiteUrl + "/admin/deleteSuggestion?id=" + obj.data.id;
				layer.confirm(
					"确认删除？", {
						btn: ['确定', '取消'],
						title: "提示"
					},
					function(index) {
						console.log(url)
						$.post({
							url: url,
							success: function(res) {
								console.log(res);
								if (res.status == 200) {
									layer.msg("删除成功");
									layer.close(index);
									setTimeout(function() {
										location.reload();
									}, 1000);
								} else {
									layer.msg("删除失败", {
										time: 1500,
										anim: 6
									});
									layer.close(index);
								}
							}
						});
						layer.close(index);
					});
			}
		});



		//按钮事件
		var $ = layui.$,
			active = {
				reload: function() { // 页面重载&模糊查询
					var search = $('#search');
					//执行重载
					table.reload('reloadTable', {
						// header:'application/x-www-form-urlencoded',
						page: {
							curr: 1 //重新从第 1 页开始
						},
						where: {
							description: search.val()
						},
					}, "data");
				},
			};



	});
})();
